<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="formData.id ? '编辑部门' : '新增部门'" :visible="showDialog" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="deptForm"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in employeeList"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button
          type="primary"
          size="small"
          @click="addSubmit"
        >确定</el-button>
        <el-button size="small" @click.native="handleClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getDeparments,
  addDeparments,
  getDeparmentsDetail,
  editDeparment
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: [Object, String],
      required: true
    }
  },
  data() {
    const checkDepartName = async(rule, value, callback) => {
      const { depts } = await getDeparments()
      console.log(depts)
      let isRepeat = false

      if (this.formData.id) {
        isRepeat = depts
          .filter((item) => item.id !== this.treeNode.id && item.pid === this.treeNode.pid)
          .some((item) => item.name === value)
      } else {
        isRepeat = depts
          .filter((item) => item.pid === this.treeNode.id)
          .some((item) => item.name === value)
      }
      isRepeat ? callback(new Error('同部门下不能重名')) : callback()
    }

    const checkDepartCode = async(rule, value, callback) => {
      const { depts } = await getDeparments()

      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.some(item => item.code === value && item.id !== this.treeNode.id)
      } else {
        isRepeat = depts.some((item) => item.code === value)
      }

      isRepeat ? callback(new Error('同部门下部门编码不能重复')) : callback()
    }

    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employeeList: [],
      rules: {
        name: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { max: 50, message: '名称长度1-50', trigger: 'blur' },
          { trigger: 'blur', validator: checkDepartName }
        ],
        code: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { max: 50, message: '不能超过50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkDepartCode }
        ],
        manager: [{ required: true, message: '此项不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '此项不能为空', trigger: 'blur' },
          { max: 300, message: '不能超过300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    this.employeeList = await getEmployeeSimple()
    // console.log(this.employeeList)
  },
  methods: {
    handleClose() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    // checkManager() {
    //   this.$refs.deptForm.validate('manager')
    // }
    async addSubmit() {
      await this.$refs.deptForm.validate()
      //   console.log({ ...this.formData, pid: this.treeNode.id })
      if (this.formData.id) {
        console.log('edit')
        await editDeparment(this.formData)
      } else {
        await addDeparments({ ...this.formData, pid: this.treeNode.id })
      }
      //   console.log(await addDeparments({ ...this.formData, pid: this.treeNode.id }))
      this.$message.success('操作成功')
      this.handleClose()
      this.$emit('addSubmit')
    },
    async getDeparmentsDetail(id) {
      this.formData = await getDeparmentsDetail(id)
      console.log(this.formData)
    }
  }
}
</script>

<style>
</style>
